package com.szgz.platform.model;

import com.baomidou.mybatisplus.annotation.*;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@TableName("user_environment")
@AllArgsConstructor
public class UserEnvironment {
    
    @TableId(value = "environment_id", type = IdType.ASSIGN_UUID)
    private String environmentId;
    
    @TableField("user_id")
    private String userId;
    
    @TableField("status")
    private String status;
    
    @TableField("ide_url")
    private String ideUrl;
    
    @TableField("jupyter_url")
    private String jupyterUrl;
    
    @TableField("redis_url")
    private String redisUrl;
    
    @TableField("mysql_url")
    private String mysqlUrl;
    
    @TableField("rabbitmq_url")
    private String rabbitmqUrl;
    
    @TableField("elasticsearch_url")
    private String elasticsearchUrl;
    
    @TableField("virtual_desktop_url")
    private String virtualDesktopUrl;
    
    @TableField("ide_password")
    private String idePassword;
    
    @TableField("jupyter_token")
    private String jupyterToken;
    
    @TableField("redis_password")
    private String redisPassword;
    
    @TableField("mysql_password")
    private String mysqlPassword;
    
    @TableField("rabbitmq_password")
    private String rabbitmqPassword;
    
    @TableField("elasticsearch_password")
    private String elasticsearchPassword;
    
    @TableField("virtual_desktop_password")
    private String virtualDesktopPassword;
    
    @TableField("ide_port")
    private Integer idePort;
    
    @TableField("jupyter_port")
    private Integer jupyterPort;
    
    @TableField("redis_port")
    private Integer redisPort;
    
    @TableField("mysql_port")
    private Integer mysqlPort;
    
    @TableField("rabbitmq_port")
    private Integer rabbitmqPort;
    
    @TableField("elasticsearch_port")
    private Integer elasticsearchPort;
    
    @TableField("virtual_desktop_port")
    private Integer virtualDesktopPort;
    
    @TableField(value = "environment_types", typeHandler = com.szgz.platform.handler.SetTypeHandler.class)
    private Set<String> environmentTypes;
    
    @TableField("created_at")
    private LocalDateTime createdAt;
    
    @TableField("last_started")
    private LocalDateTime lastStarted;
    
    @TableField("container_id")
    private String containerId;
    
    @TableField("network_id")
    private String networkId;
    
    @TableField("volume_id")
    private String volumeId;
    
    @TableLogic
    @TableField("deleted")
    private Boolean deleted;
    
    // 构造函数
    public UserEnvironment() {
        this.deleted = false;
        this.createdAt = LocalDateTime.now();
    }
    
    // 构造函数
    public UserEnvironment(String userId, Set<EnvironmentType> environmentTypes) {
        this();
        this.userId = userId;
        this.setEnvironmentTypes(environmentTypes);
        this.status = "CREATING";
    }

    public UserEnvironment(String userId, Map<String, Integer> ports, Set<EnvironmentType> mergedTypes) {
        this();
        this.userId = userId;
        this.setEnvironmentTypes(mergedTypes);
        this.status = "CREATING";
        
        // 设置端口信息
        if (ports != null) {
            this.idePort = ports.get("ide");
            this.jupyterPort = ports.get("jupyter");
            this.redisPort = ports.get("redis");
            this.mysqlPort = ports.get("mysql");
            this.rabbitmqPort = ports.get("rabbitmq");
            this.elasticsearchPort = ports.get("elasticsearch");
            this.virtualDesktopPort = ports.get("virtual-desktop");
        }
        
        // 设置密码和token
        this.idePassword = generateUserPassword(userId);
        this.jupyterToken = generateJupyterToken(userId);
        this.redisPassword = generateRedisPassword(userId);
        this.mysqlPassword = generateMysqlPassword(userId);
        this.rabbitmqPassword = generateRabbitmqPassword(userId);
        this.elasticsearchPassword = generateElasticsearchPassword(userId);
        this.virtualDesktopPassword = generateVirtualDesktopPassword(userId);
    }
    
    // 生成密码的辅助方法 - 统一使用123456
    private String generateUserPassword(String userId) {
        return "123456";
    }
    
    private String generateJupyterToken(String userId) {
        return "123456";
    }
    
    private String generateRedisPassword(String userId) {
        return "123456";
    }
    
    private String generateMysqlPassword(String userId) {
        return "123456";
    }
    
    private String generateRabbitmqPassword(String userId) {
        return "123456";
    }
    
    private String generateElasticsearchPassword(String userId) {
        return "123456";
    }
    
    private String generateVirtualDesktopPassword(String userId) {
        return "123456";
    }
    
    // 安全的getter和setter
    public Set<EnvironmentType> getEnvironmentTypes() {
        if (environmentTypes == null) return Set.of();
        return environmentTypes.stream()
                .map(EnvironmentType::fromCode)
                .collect(Collectors.toSet());
    }
    
    public void setEnvironmentTypes(Set<EnvironmentType> environmentTypes) {
        this.environmentTypes = environmentTypes != null ? 
            environmentTypes.stream()
                .map(EnvironmentType::getCode)
                .collect(Collectors.toSet()) : 
            new HashSet<>();
    }
    
    // 添加环境类型
    public void addEnvironmentType(EnvironmentType type) {
        if (this.environmentTypes == null) {
            this.environmentTypes = new HashSet<>();
        }
        this.environmentTypes.add(type.getCode());
    }
    
    // 移除环境类型
    public void removeEnvironmentType(EnvironmentType type) {
        if (this.environmentTypes != null) {
            this.environmentTypes.remove(type.getCode());
        }
    }
    
    // 检查是否包含特定环境类型
    public boolean hasEnvironmentType(EnvironmentType type) {
        return this.environmentTypes != null && this.environmentTypes.contains(type.getCode());
    }
    
    // 获取环境类型数量
    public int getEnvironmentTypeCount() {
        return this.environmentTypes != null ? this.environmentTypes.size() : 0;
    }
    
    // 基本的getter和setter方法
    public String getEnvironmentId() { return environmentId; }
    public void setEnvironmentId(String environmentId) { this.environmentId = environmentId; }
    
    public String getUserId() { return userId; }
    public void setUserId(String userId) { this.userId = userId; }
    
    public String getStatus() { return status; }
    public void setStatus(String status) { this.status = status; }
    
    public String getIdeUrl() { return ideUrl; }
    public void setIdeUrl(String ideUrl) { this.ideUrl = ideUrl; }
    
    public String getJupyterUrl() { return jupyterUrl; }
    public void setJupyterUrl(String jupyterUrl) { this.jupyterUrl = jupyterUrl; }
    
    public String getRedisUrl() { return redisUrl; }
    public void setRedisUrl(String redisUrl) { this.redisUrl = redisUrl; }
    
    public String getMysqlUrl() { return mysqlUrl; }
    public void setMysqlUrl(String mysqlUrl) { this.mysqlUrl = mysqlUrl; }
    
    public String getRabbitmqUrl() { return rabbitmqUrl; }
    public void setRabbitmqUrl(String rabbitmqUrl) { this.rabbitmqUrl = rabbitmqUrl; }
    
    public String getElasticsearchUrl() { return elasticsearchUrl; }
    public void setElasticsearchUrl(String elasticsearchUrl) { this.elasticsearchUrl = elasticsearchUrl; }
    
    public String getVirtualDesktopUrl() { return virtualDesktopUrl; }
    public void setVirtualDesktopUrl(String virtualDesktopUrl) { this.virtualDesktopUrl = virtualDesktopUrl; }
    
    public String getIdePassword() { return idePassword; }
    public void setIdePassword(String idePassword) { this.idePassword = idePassword; }
    
    public String getJupyterToken() { return jupyterToken; }
    public void setJupyterToken(String jupyterToken) { this.jupyterToken = jupyterToken; }
    
    public String getRedisPassword() { return redisPassword; }
    public void setRedisPassword(String redisPassword) { this.redisPassword = redisPassword; }
    
    public String getMysqlPassword() { return mysqlPassword; }
    public void setMysqlPassword(String mysqlPassword) { this.mysqlPassword = mysqlPassword; }
    
    public String getRabbitmqPassword() { return rabbitmqPassword; }
    public void setRabbitmqPassword(String rabbitmqPassword) { this.rabbitmqPassword = rabbitmqPassword; }
    
    public String getElasticsearchPassword() { return elasticsearchPassword; }
    public void setElasticsearchPassword(String elasticsearchPassword) { this.elasticsearchPassword = elasticsearchPassword; }
    
    public String getVirtualDesktopPassword() { return virtualDesktopPassword; }
    public void setVirtualDesktopPassword(String virtualDesktopPassword) { this.virtualDesktopPassword = virtualDesktopPassword; }
    
    public Integer getIdePort() { return idePort; }
    public void setIdePort(Integer idePort) { this.idePort = idePort; }
    
    public Integer getJupyterPort() { return jupyterPort; }
    public void setJupyterPort(Integer jupyterPort) { this.jupyterPort = jupyterPort; }
    
    public Integer getRedisPort() { return redisPort; }
    public void setRedisPort(Integer redisPort) { this.redisPort = redisPort; }
    
    public Integer getMysqlPort() { return mysqlPort; }
    public void setMysqlPort(Integer mysqlPort) { this.mysqlPort = mysqlPort; }
    
    public Integer getRabbitmqPort() { return rabbitmqPort; }
    public void setRabbitmqPort(Integer rabbitmqPort) { this.rabbitmqPort = rabbitmqPort; }
    
    public Integer getElasticsearchPort() { return elasticsearchPort; }
    public void setElasticsearchPort(Integer elasticsearchPort) { this.elasticsearchPort = elasticsearchPort; }
    
    public Integer getVirtualDesktopPort() { return virtualDesktopPort; }
    public void setVirtualDesktopPort(Integer virtualDesktopPort) { this.virtualDesktopPort = virtualDesktopPort; }
    
    public LocalDateTime getCreatedAt() { return createdAt; }
    public void setCreatedAt(LocalDateTime createdAt) { this.createdAt = createdAt; }
    
    public LocalDateTime getLastStarted() { return lastStarted; }
    public void setLastStarted(LocalDateTime lastStarted) { this.lastStarted = lastStarted; }
    
    public String getContainerId() { return containerId; }
    public void setContainerId(String containerId) { this.containerId = containerId; }
    
    public String getNetworkId() { return networkId; }
    public void setNetworkId(String networkId) { this.networkId = networkId; }
    
    public String getVolumeId() { return volumeId; }
    public void setVolumeId(String volumeId) { this.volumeId = volumeId; }
    
    public Boolean getDeleted() { return deleted; }
    public void setDeleted(Boolean deleted) { this.deleted = deleted; }
    
    @Override
    public String toString() {
        return "UserEnvironment(" +
                "environmentId=" + environmentId +
                ", userId=" + userId +
                ", status=" + status +
                ", ideUrl=" + ideUrl +
                ", jupyterUrl=" + jupyterUrl +
                ", redisUrl=" + redisUrl +
                ", mysqlUrl=" + mysqlUrl +
                ", rabbitmqUrl=" + rabbitmqUrl +
                ", elasticsearchUrl=" + elasticsearchUrl +
                ", virtualDesktopUrl=" + virtualDesktopUrl +
                ", idePassword=" + idePassword +
                ", jupyterToken=" + jupyterToken +
                ", redisPassword=" + redisPassword +
                ", mysqlPassword=" + mysqlPassword +
                ", rabbitmqPassword=" + rabbitmqPassword +
                ", elasticsearchPassword=" + elasticsearchPassword +
                ", virtualDesktopPassword=" + virtualDesktopPassword +
                ", idePort=" + idePort +
                ", jupyterPort=" + jupyterPort +
                ", redisPort=" + redisPort +
                ", mysqlPort=" + mysqlPort +
                ", rabbitmqPort=" + rabbitmqPort +
                ", elasticsearchPort=" + elasticsearchPort +
                ", virtualDesktopPort=" + virtualDesktopPort +
                ", environmentTypes=" + environmentTypes +
                ", createdAt=" + createdAt +
                ", lastStarted=" + lastStarted +
                ", containerId=" + containerId +
                ", networkId=" + networkId +
                ", volumeId=" + volumeId +
                ", deleted=" + deleted +
                ")";
    }
    
    // 环境类型枚举
    public enum EnvironmentType {
        IDE("IDE", "集成开发环境"),
        JUPYTER("JUPYTER", "Jupyter Notebook"),
        JUPYTER_LIGHT("JUPYTER_LIGHT", "轻量级Jupyter Lab"),
        REDIS("REDIS", "Redis缓存"),
        MYSQL("MYSQL", "MySQL数据库"),
        RABBITMQ("RABBITMQ", "RabbitMQ消息队列"),
        ELASTICSEARCH("ELASTICSEARCH", "Elasticsearch搜索引擎"),
        VIRTUAL_DESKTOP("VIRTUAL_DESKTOP", "虚拟桌面");
        
        private final String code;
        private final String description;
        
        EnvironmentType(String code, String description) {
            this.code = code;
            this.description = description;
        }
        
        public String getCode() {
            return code;
        }
        
        public String getDescription() {
            return description;
        }
        
        public static EnvironmentType fromCode(String code) {
            for (EnvironmentType type : values()) {
                if (type.code.equals(code)) {
                    return type;
                }
            }
            throw new IllegalArgumentException("Unknown environment type: " + code);
        }
        
        @Override
        public String toString() {
            return this.code;
        }
    }
} 